#!/bin/sh
# - runtime
# - pattern
# - mapsize
# - setsize
# - total_setsize
# - shape
# - nr_processes
# - nr_threads
# - timestamp
# - cold
# - access
# - delay
# - initialize
# - offset
# - quiet
# - ratio
# - threshold

. "$LKP_SRC/lib/common.sh"
. "$LKP_SRC/lib/reproduce-log.sh"

: "${runtime:=300}"
: "${nr_processes:=1}"

set_opt()
{
	opt=
	if [ -n "$total_setsize" ] && [ -z "$setsize" ]; then
		nr_inst=${nr_instances:-1}
		setsize=$((total_setsize / nr_inst / nr_processes))
	fi
	if [ -n "$setsize" ]; then
		opt="${opt} -s$((setsize/1024/1024))"
		[ -z "$mapsize" ] && mapsize="$setsize"
	fi
	[ -n "$mapsize" ] && {
		mapsize=$((mapsize/1024/1024))
		[ "$mapsize" -eq 0 ] && mapsize=256
		opt="${opt} -m${mapsize}"
	}
	[ -n "$pattern" ] && opt="${opt} -p${pattern}"
	[ -n "$shape" ] && opt="${opt} -e${shape}"
	[ -n "$nr_threads" ] && opt="${opt} -j${nr_threads}"
	[ -n "$timestamp" ] && opt="${opt} -t${timestamp}"
	parse_bool -q "$cold" && opt="${opt} -c"
	[ -n "$access" ] && opt="${opt} -a${access}"
	[ -n "$delay" ] && opt="${opt} -d${delay}"
	parse_bool -q "$initialize" && opt="${opt} -i"
	[ -n "$offset" ] && opt="${opt} -o${offset}"
	parse_bool -q "$quiet" && opt="${opt} -q"
	[ -n "$ratio" ] && opt="${opt} -r${ratio}"
	[ -n "$threshold" ] && opt="${opt} -h${threshold}"
}

set_opt

: "${instance_id:=1}"
pmb_tmp_dir="$TMP/$instance_id/pmbench"
rm -rf "$pmb_tmp_dir"
mkdir -p "$pmb_tmp_dir"

pmbench=/usr/local/bin/pmbench

for i in $(seq "$nr_processes"); do
	log_echo "$exec_prefix $pmbench $opt $runtime > $pmb_tmp_dir/pmbench.$i &"
	$pmbench $opt $runtime > "$pmb_tmp_dir/pmbench.$i" &
	echo $! >> $TMP/pid-tests
done

wait

cat "$pmb_tmp_dir"/pmbench.*
